---
title: Using aliases
---

When generating code, Apollo Kotlin uses the name of your fields to generate matching typesafe models using a flat hierarchy. The following query:

```graphql title="GetContact.graphql"
query GetContact {
  contact(name: "Byron") {
    # The generated model will be named `HomeAddress`
    homeAddress {
      street
    }
    # The generated model will be named `WorkAddress`
    workAddress {
      country
    }
  }
}
```

will generate models like this:

```kotlin
class GetContact {
  class Data { ... }
  class Contact { ... }
  class HomeAddress { ... }
  class WorkAddress { ... }
}
```

The driving principles behind the naming conventions are:
* Use the field name for model names: we considered using the schema type name instead but that would make a lot of conflicts. In the example above, for an example, we would have two different `Address` classes.
* Keep a flat hierarchy of classes: we considered using nested classes but that can lead to very long names and a flat hierarchy is easier to read.
* Singularize the field name when it is a list: if your query contains a List fields, such as `nodes` for an example, it makes more sense to generate a `List<Node>` than a `List<Nodes>`.

These principles work great for the vast majority of cases but can't account for name clashes or peculiar singularization rules. To work around these, you can use GraphQL [aliases](https://graphql.org/learn/queries/#aliases).

## Using aliases to avoid name clashes

Since the class hierarchy is flat, having two fields with the same name in two different parts of the query tree could lead to name clashes. Having relay-style pagination with lots of `nodes` or `edges` fields, might typically trigger name clashes. To avoid them, Apollo Kotlin will append a numeric suffix to your models:

```graphql title="GetContact.graphql"
query GetContacts {
  contact(name: "Byron") {
    # The generated model will be named `HomeAddress`
    homeAddress {
      street
    }
  }
  emergencyContact {
    # The generated model will be named `HomeAddress1` by default
    homeAddress {
      street
    }
  }
}
```

Suffixing avoids the name clash but feels a bit out of place, and it's hard to remember what `HomeAddress1` is about. To work around this issue, you can alias the field:

```graphql title="GetContact.graphql"
query GetContacts {
  contact(name: "Byron") {
    # The generated model will be named `HomeAddress`
    homeAddress {
      street
    }
  }
  emergencyContact {
    # With an alias, the generated model will be named `EmergencyAddress`
    emergencyAddress: homeAddress {
      street
    }
  }
}
```

## Using aliases to fix singularization rules

The [singularization code](https://github.com/apollographql/apollo-kotlin/blob/d77b73f6f387ab474045f0db3c568d251ef55932/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/Inflector.kt) works most of the time but does not contain all the exceptions in the English dictionary.

If you encounter one, please let us know, so we can put a valid example here (we usually fix those issues as we go but there certainly are a few left in the wild). Until we fix it in the singularization code, you can use an alias to control the name of the model in your code.
